package com.wind.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wind.activity.ArticleActivity;
import com.wind.adapter.ArticleListAdapter;
import com.wind.base.BaseFragment;
import com.wind.beans.ArticleListBean;
import com.wind.soblogin.HttpManager;
import com.debug.soblogin.R;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class HomeFragment extends BaseFragment {
    private RecyclerView mList;
    private ArticleListAdapter mAdapter;
    private ArrayList<ArticleListBean.DataDTO.ContentsDTO> mData;

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {

        return inflater.inflate(R.layout.fragment_home, container, false);
    }


    public void itemClick() {

        mAdapter.setOnItemClickListener(new ArticleListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ArticleListBean.DataDTO.ContentsDTO contentsDTO = mData.get(position);
                String id = contentsDTO.getId();
                String article_title = contentsDTO.getTitle();
                String time = contentsDTO.getCreateTime().substring(5, 10) + "  " + contentsDTO.getCreateTime().substring(11, 16);
                int viewCount = contentsDTO.getViewCount();
                String label = contentsDTO.getLabel();

                Intent intent = new Intent(getContext(), ArticleActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("article_title", article_title);
                intent.putExtra("time", time);
                intent.putExtra("viewCount", viewCount + "");
                intent.putExtra("label", label);
                startActivity(intent);

            }
        });
    }

    @Override
    protected void initView(View rootView) {

        mList = rootView.findViewById(R.id.article_list);
        super.initView(rootView);

        //开始检测是否需要升级
       initAppUpdate();
//        XUpdate.newBuild(Objects.requireNonNull(getContext())).updateUrl("http://mp.lolxiaoguo.cn/apk/wind_update.json").update();

    }
    private void initAppUpdate() {


    }




    @Override
    protected void initData() {

        mData = new ArrayList<>();

        HttpManager
                .getSobApi()
                .getArticleList()
                .enqueue(new Callback<ArticleListBean>() {
                    @Override
                    public void onResponse(Call<ArticleListBean> call, Response<ArticleListBean> response) {
                        //Toast.makeText(getContext(), "接口响应成功", Toast.LENGTH_LONG).show();
                        ArticleListBean data = response.body();
                        for (int i = 0; i < data.getData().getTotalCount(); i++) {
                            mData.add(data.getData().getContents().get(i));
                        }

                        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                        mList.setLayoutManager(layoutManager);

                        //创建适配器
                        mAdapter = new ArticleListAdapter(mData);
                        //设置到recycle里面去
                        mList.setAdapter(mAdapter);
                        itemClick();
                    }

                    @Override
                    public void onFailure(Call<ArticleListBean> call, Throwable t) {

                    }


                });
    }
}
